package com.bluemsun.naturallanguageprocessing.controller;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.entity.Feedback;
import com.bluemsun.naturallanguageprocessing.entity.User;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.exception.feedback.GetFeedbackException;
import com.bluemsun.naturallanguageprocessing.service.IFeedbackService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import com.bluemsun.naturallanguageprocessing.util.DateTimeUtil;
import com.bluemsun.naturallanguageprocessing.util.JsonUtil;
import com.bluemsun.naturallanguageprocessing.vo.FeedbackIdsVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author :hucl
 * @describe :用户反馈
 */
@Validated
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    private static final int ADMINISTRATOR_ID = -1;

    @Autowired
    private IFeedbackService iFeedbackService;

    @Autowired
    private ConductRedisUtil conductRedisUtil;

    @PostMapping("/add")
    public ServerResponse addFeedback(@Validated @RequestBody Feedback feedback, HttpServletRequest request){
        //TODO:后期添加 aop token校验判断用户是否登录，redis用户信息是否过期交由getUserFromRedis方法中的异常判断处理
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        feedback.setId(user.getId());
        feedback.setId(2);
        feedback.setDate(DateTimeUtil.dateToStr(new Date()));
        return iFeedbackService.userAddFeedBack(feedback);
    }

    @GetMapping("/get")
    public ServerResponse getFeedback(HttpServletRequest request,@RequestParam("pageNum") @Min(1) int pageNum, @RequestParam("pageSize") int pageSize){
        //TODO:后期添加 aop token校验判断用户是否登录，redis用户信息是否过期交由getUserFromRedis方法中的异常判断处理
        //TODO:和前端商量pageSize的默认值
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        PageInfo<Feedback> feedbackList = null;
        if(user.getRole()==1){
            feedbackList = iFeedbackService.getFeedbackList(user.getId(), pageNum, pageSize);
        }else
            feedbackList = iFeedbackService.getFeedbackList(ADMINISTRATOR_ID, pageNum, pageSize);
        if (feedbackList.getSize()==0){
            throw new GetFeedbackException("反馈信息查询为空");
        }
        return ServerResponse.createBySuccess("获取反馈列表成功",feedbackList);
    }

    @PostMapping("/administratorDeleteFeedback")
    public ServerResponse administratorDeleteFeedback(@RequestBody FeedbackIdsVO feedbackIdsVO){
        //TODO:后期添加两个aop 一个用于token校验判断用户是否登录，另一个用于user校验判断redis里面用户信息是否以及过期
        return iFeedbackService.administratorDeleteFeedbackSet(feedbackIdsVO.getIds());
    }
}
